package com.yoyo.ad.toutiao;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTFeedAd;
import com.bytedance.sdk.openadsdk.TTInteractionAd;
import com.bytedance.sdk.openadsdk.TTNativeAd;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.yoyo.ad.confusion.CommonTTADClickListener;
import com.yoyo.ad.main.AdModelType;
import com.yoyo.ad.main.IAdClick;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

import static com.yoyo.ad.confusion.AdManage.TOU_TIAO;


class ToutiaoYoYoAd implements YoYoAd {


//    ToutiaoYoYoAd(TTNativeAd ttAdNative) {
//        this.mTTAdNative = ttAdNative;
//    }

    private IAdClick mIAdClick;
    private boolean isNative;

    ToutiaoYoYoAd(TTFeedAd ttFeedAd, IAdClick adClick) {
        this.mTTFeedAd = ttFeedAd;
        this.mIAdClick = adClick;
    }

    private TTNativeAd mTTAdNative;
    private TTFeedAd mTTFeedAd;
    private TTInteractionAd mTTInteractionAd;
    private TTNativeExpressAd mTTNativeExpressAd;

    public ToutiaoYoYoAd(TTInteractionAd ttInteractionAd) {
        mTTInteractionAd = ttInteractionAd;
    }

    public ToutiaoYoYoAd(TTNativeExpressAd TTNativeExpressAd) {
        mTTNativeExpressAd = TTNativeExpressAd;
        isNative = true;
    }

    @Override
    public int getSource() {
        return TOU_TIAO;
    }

    @Override
    public String getTitle() {
        if (mTTAdNative != null) {
            return mTTAdNative.getTitle();
        }
        if (mTTFeedAd != null) {
            return mTTFeedAd.getTitle();
        }
        return "";
    }

    @Override
    public String getDescription() {
        if (mTTAdNative != null) {
            return mTTAdNative.getDescription();
        }
        if (mTTFeedAd != null) {
            return mTTFeedAd.getDescription();
        }
        return "";
    }

    @Override
    public Bitmap getLogo() {
        if (mTTAdNative != null) {
            return mTTAdNative.getAdLogo();
        }
        if (mTTFeedAd != null) {
            return mTTFeedAd.getAdLogo();
        }
        return null;
    }

    @Override
    public String getIconUrl() {
        if (mTTAdNative != null && mTTAdNative.getIcon() != null) {
            return mTTAdNative.getIcon().getImageUrl();
        }
        if (mTTFeedAd != null && mTTFeedAd.getIcon() != null) {
            return mTTFeedAd.getIcon().getImageUrl();
        }
        return "";
    }

    @Override
    public String getImgUrl1() {
        if (mTTAdNative != null && mTTAdNative.getImageList() != null) {
            return mTTAdNative.getImageList().get(0).getImageUrl();
        }
        if (mTTFeedAd != null && mTTFeedAd.getImageList() != null) {
            return mTTFeedAd.getImageList().get(0).getImageUrl();
        }
        return "";
    }

    @Override
    public String getImgUrl2() {
        if (mTTAdNative != null && mTTAdNative.getImageList() != null) {
            return mTTAdNative.getImageList().size() > 1 ?
                    mTTAdNative.getImageList().get(1).getImageUrl() : null;
        }
        if (mTTFeedAd != null && mTTFeedAd.getImageList() != null) {
            return mTTFeedAd.getImageList().size() > 1 ?
                    mTTFeedAd.getImageList().get(1).getImageUrl() : null;
        }
        return null;
    }

    @Override
    public String getImgUrl3() {
        if (mTTAdNative != null && mTTAdNative.getImageList() != null) {
            return mTTAdNative.getImageList().size() > 2 ?
                    mTTAdNative.getImageList().get(2).getImageUrl() : null;
        }
        if (mTTFeedAd != null && mTTFeedAd.getImageList() != null) {
            return mTTFeedAd.getImageList().size() > 2 ?
                    mTTFeedAd.getImageList().get(2).getImageUrl() : null;
        }
        return null;
    }

    @Override
    public String getBrandName() {
        if (mTTAdNative != null) {
            return StringUtil.notNull(mTTAdNative.getSource(), "头条");
        }
        if (mTTFeedAd != null) {
            return StringUtil.notNull(mTTFeedAd.getSource(), "头条");
        }
        return "";
    }

    @Override
    public int getModel() {
        if (mTTAdNative != null) {
            if (mTTAdNative.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO ||
                    mTTAdNative.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO_VERTICAL) {
                return AdModelType.MODEL_VIDEO;
            } else if (mTTAdNative.getImageMode() == TTAdConstant.IMAGE_MODE_UNKNOWN) {
                return AdModelType.MODEL_UNKNOW;
            } else {
                return AdModelType.MODEL_IMAGE;
            }
        }
        if (mTTFeedAd != null) {
            if (mTTFeedAd.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO ||
                    mTTFeedAd.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO_VERTICAL) {
                return AdModelType.MODEL_VIDEO;
            } else if (mTTFeedAd.getImageMode() == TTAdConstant.IMAGE_MODE_UNKNOWN) {
                return AdModelType.MODEL_UNKNOW;
            } else {
                return AdModelType.MODEL_IMAGE;
            }
        }
        return AdModelType.MODEL_UNKNOW;
    }

    @Override
    public View getView() {
        if (mTTFeedAd != null) {
            return mTTFeedAd.getAdView();
        }
        if (mTTNativeExpressAd != null) {
            return mTTNativeExpressAd.getExpressAdView();
        }
        return null;
    }

    @Override
    public boolean isNativeExpress() {
        return isNative;
    }

    @Override
    public void exposure(View view) {
        if (mTTNativeExpressAd != null) {
            mTTNativeExpressAd.render();
        }
    }

    @Override
    public void onAdClicked(View view, ViewGroup viewGroup) {
        if (mTTAdNative != null && viewGroup != null) {
            //可以被点击的view, 也可以把convertView放进来意味item可被点击
            List<View> clickViewList = new ArrayList<>();
            clickViewList.add(view);
            //触发创意广告的view（点击下载或拨打电话）
            List<View> creativeViewList = new ArrayList<>();
            creativeViewList.add(view);
            //如果需要点击图文区域也能进行下载或者拨打电话动作，请将图文区域的view传入
            mTTAdNative.registerViewForInteraction(viewGroup, clickViewList, creativeViewList, new CommonTTADClickListener() {
                @Override
                public void onAdCreativeClick(View view, TTNativeAd ttNativeAd) {
                    super.onAdClicked(view, ttNativeAd);
                    if (mIAdClick != null) {
                        mIAdClick.onAdClick(view);
                    }
                }

                @Override
                public void onAdShow(TTNativeAd ttNativeAd) {
                    super.onAdShow(ttNativeAd);
                    if (mIAdClick != null) {
                        mIAdClick.onAdShow();
                    }
                }
            });
        }
        if (mTTFeedAd != null && viewGroup != null) {
            //可以被点击的view, 也可以把convertView放进来意味item可被点击
            List<View> clickViewList = new ArrayList<>();
            clickViewList.add(view);
            //触发创意广告的view（点击下载或拨打电话）
            List<View> creativeViewList = new ArrayList<>();
            creativeViewList.add(view);
            //如果需要点击图文区域也能进行下载或者拨打电话动作，请将图文区域的view传入
            mTTFeedAd.registerViewForInteraction(viewGroup, clickViewList, creativeViewList, new CommonTTADClickListener() {
                @Override
                public void onAdCreativeClick(View view, TTNativeAd ttNativeAd) {
                    super.onAdClicked(view, ttNativeAd);
                    if (mIAdClick != null) {
                        mIAdClick.onAdClick(view);
                    }
                }

                @Override
                public void onAdShow(TTNativeAd ttNativeAd) {
                    super.onAdShow(ttNativeAd);
                    if (mIAdClick != null) {
                        mIAdClick.onAdShow();
                    }
                }
            });
        }
    }

    @Override
    public void show(Activity activity) {
        if (mTTInteractionAd != null) {
            mTTInteractionAd.showInteractionAd(activity);
        }
        if (mTTNativeExpressAd != null) {
            mTTNativeExpressAd.showInteractionExpressAd(activity);
        }
    }

    @Override
    public void release() {
        if (mTTNativeExpressAd != null)
            mTTNativeExpressAd.destroy();
    }
}
